 
/*
 * Copyright (c) 2011-2012 Tygron Serious Gaming
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of 'Swing to Nifty Converter' nor the names of its contributors
 *   may be used to endorse or promote products derived from this software
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
  
package nl.tygron.niftyconverter.elements;

import java.awt.Color;
import java.awt.Font;
import java.util.logging.Logger;

/**
 * @author Raymond Weijermars, William van Velzen
 * @Specialism GUI
 * @Maintainer Jeroen Warmerdam
 */

public class NImageElement extends NElement {

	private static final Logger logger = Logger.getLogger(NImageElement.class.getName());

	private final static String PANEL_ELEMENT = "Panel";
	private final static String WIDTH_ELEMENT = "width";
	private final static String HEIGHT_ELEMENT = "height";
	private final static String LEFT = "left";
	private final static String CENTER = "center";
	private final static String ABSOLUTE = "absolute";
	private final static String CHILDLAYOUT_ELEMENT = "childLayout";

	protected NLabelElement nLabelElement;
	protected NImageElement childImageElement;

	public NImageElement(String id, int x, int y, int panelWidth, int panelHeight, int iconWidth, int iconHeight,
	        NiftyStructure nStructure, String imageFileName, String align, String valign, boolean isButton) {

		super(id + PANEL_ELEMENT, x, y, panelWidth, panelHeight, nStructure.newElement("panel"));

		if (imageFileName.equals("")) {
			logger.warning("image file is empty!: " + id);
		}

		childImageElement = new NImageElement(id, nStructure, imageFileName, align, valign);

		if (isButton) {
			childImageElement.addAttribute(WIDTH_ELEMENT, iconWidth + "");
			childImageElement.addAttribute(HEIGHT_ELEMENT, iconHeight + "");
		} else {
			childImageElement.addAttribute(WIDTH_ELEMENT, panelWidth + "");
			childImageElement.addAttribute(HEIGHT_ELEMENT, panelHeight + "");
		}

		this.addChild(childImageElement);
		this.addAttribute(CHILDLAYOUT_ELEMENT, CENTER);
	}

	// an image with label ontop of the image. often used for buttons etc..
	public NImageElement(String id, int x, int y, int labelWidth, int labelHeight, NiftyStructure nStructure, String imageFileName,
	        String labelText, Font font, Color labelColor, String align, String valign, boolean isButton) {

		super(id + "Panel", x, y, labelWidth, labelHeight, nStructure.newElement("panel"));
		this.addAttribute(CHILDLAYOUT_ELEMENT, CENTER);
		childImageElement = new NImageElement(id, nStructure, imageFileName, align, valign);
		this.addChild(childImageElement);
		if (align.equals(CENTER)) {
			childImageElement.addAttribute(CHILDLAYOUT_ELEMENT, CENTER);
		} else {
			childImageElement.addAttribute(CHILDLAYOUT_ELEMENT, ABSOLUTE);
		}

		int xOffset = 0;
		if (LEFT.equals(align)) {
			xOffset = 30;
		}

		nLabelElement = new NLabelElement(id + "Label", xOffset, 0, labelWidth, labelHeight, nStructure, labelText, font, labelColor,
		        align, valign, true);
		childImageElement.addChild(nLabelElement);
	}

	public NImageElement(String id, int x, int y, int panelWidth, int panelHeight, NiftyStructure nStructure, String imageFileName,
	        String align, String valign, boolean isButton) {

		super(id + PANEL_ELEMENT, x, y, panelWidth, panelHeight, nStructure.newElement("panel"));
		childImageElement = new NImageElement(id, nStructure, imageFileName, align, valign);
		// if (isButton) {
		childImageElement.addAttribute(WIDTH_ELEMENT, panelWidth + "");
		childImageElement.addAttribute(HEIGHT_ELEMENT, panelHeight + "");
		// }
		this.addChild(childImageElement);
		this.addAttribute(CHILDLAYOUT_ELEMENT, CENTER);
	}

	private NImageElement(String id, NiftyStructure nStructure, String imageFileName, String align, String valign) {

		super(id + "Image", nStructure.newElement("image"));
		if (!"".equals(imageFileName)) {
			this.addAttribute("filename", imageFileName);
		}
		this.addAttribute("align", align);
		this.addAttribute("valign", valign);
	}

}
